package cn.relx.service.service.impl;

import cn.relx.basic.util.BaiduAuditUtils;
import cn.relx.pet.domain.Pet;
import cn.relx.pet.domain.PetOnlineAuditLog;
import cn.relx.service.domain.Product;
import cn.relx.service.domain.ProductDetail;
import cn.relx.service.domain.ProductOnlineAuditLog;
import cn.relx.service.mapper.OrderProductMapper;
import cn.relx.service.mapper.ProductDetailMapper;
import cn.relx.service.mapper.ProductMapper;
import cn.relx.service.mapper.ProductOnlineAuditLogMapper;
import cn.relx.service.service.IProductService;
import cn.relx.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author relx
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Autowired
    private OrderProductMapper orderProductMapper;

    /**
     * 重写保存ProductDetail表数据
     * @param product
     */
    @Override
    public void add(Product product) {
        super.add(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.add(detail);
    }

    /**
     * 上架
     * @param ids
     */
    @Override
    @Transactional
    public void onsale(List<Long> ids) {
        Map<Long, Product> productMaps = productMapper.findAll().stream().filter(pet -> pet.getState() == 0).collect(Collectors.toMap(Product::getId, product -> product));
        Date date = new Date();
        for (Long id : ids) {
            Product product = productMaps.get(id);
            StringBuilder sb = new StringBuilder();
            // 不等于null,说明数据本身就是下架的
            if (product != null){
                if (!BaiduAuditUtils.TextCensor(product.getName())){
                    sb.append("宠物上架百度AI审核名称失败，宠物名称为：" + product.getName() + ";");
                }
                if (product.getResources().split(",").length > 1){
                    if (!BaiduAuditUtils.ImgCensor(product.getResources().split(",")[0])){
                        sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + product.getResources() + ";");
                    }
                }else {
                    if (!BaiduAuditUtils.ImgCensor(product.getResources())){
                        sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + product.getResources() + ";");
                    }
                }
                if (sb.length() > 0){
                    onsaleAndOffsale(date, id, sb,0);
                    continue;
                }else {
                    sb.append("上架成功！");
                    onsaleAndOffsale(date, id, sb,1);
                }


                product.setState(1L);
                product.setOnsaletime(date);
                productMapper.update(product);
            }
        }
    }

    private void onsaleAndOffsale(Date date, Long id, StringBuilder sb,Integer state) {
        ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
        productOnlineAuditLog.setAuditTime(date);
        productOnlineAuditLog.setProductId(id);
        productOnlineAuditLog.setNote(sb.toString());
        productOnlineAuditLog.setState(state);
        productOnlineAuditLogMapper.add(productOnlineAuditLog);
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        productMapper.batchOffsale(ids);
    }

    @Override
    public Product getPetDetail(Long id) {
        return productMapper.getPetDetail(id);
    }
}
